package org.paniit.welcometoportland;

import java.io.IOException;
import java.util.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.SAXException;

class ServerInterface
{
private String xmlUriBase = "http://192.168.15.101:3000";

// /allcountries.xml 

public Document GetCountryNames(Map<String,String> names)
{
    String xmlUri = "/allcountries.xml";
    return GetNames(xmlUri,names);
}
// /alllanguages.xml

public Document GetLanguageNames(Map<String,String> names)
{
    String xmlUri = "/alllanguages.xml";
    return GetNames(xmlUri,names);
}
// /needcategories.xml?priority=x ..... where x is 1 or 2


public Document GetRootCategories(Map<String,String> names, String priority)
{
    String xmlUri = "/needcategories.xml?priority=" + priority;
    return GetNames(xmlUri,names);
}

// /categories/selected.xml?root_category=xx .....where xx is ID for RootCategory

public Document GetCategories(Map<String,String> names, String rootCategoryID)
{
    String xmlUri = "/categories/selected.xml?root_category=" + rootCategoryID;
    return GetNames(xmlUri,names);
}

// /resources/selected.xml?category=xx&country=yy&language=zz....
// where xx is ID for category, yy is ID of country & zz is ID of language

public Document GetResources(String category, String country, String language, int stage, Map<String,String> names, Map<String,String> pages)
{
    String xmlUri = "/categories/selected.xml?category=" + category + "&country=" + country  + "&language=" + language;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setCoalescing(true);
    dbf.setExpandEntityReferences(true);
    

    String xmlFullUri = xmlUriBase + xmlUri;

    Document doc = GetDocument(dbf, xmlFullUri);
    
    Node rootNode = doc.getFirstChild();

    NodeList children = rootNode.getChildNodes();

    for(int i = 0; i < children.getLength(); i++)
    {
      Node aNode = children.item(i);
      NamedNodeMap attrs = aNode.getAttributes();
      String id = attrs.getNamedItem("id").getNodeValue();
      String name = attrs.getNamedItem("title").getNodeValue();
      String page = attrs.getNamedItem("wiki_page").getNodeValue();
      String min_stage = attrs.getNamedItem("min_stage").getNodeValue();
      String max_stage = attrs.getNamedItem("max_stage").getNodeValue();
      if(stage > Integer.parseInt(min_stage) && stage < Integer.parseInt(max_stage))
      {
          names.put(id,name);
          pages.put(id,page);
    
      }
    }
    return doc;
}

Document GetDocument(DocumentBuilderFactory dbf, String xmlFullUri )
{
    Document doc = null;
    try{
        DocumentBuilder builder = dbf.newDocumentBuilder();
        doc = builder.parse(xmlFullUri);
    }
    catch(IOException ex)
    {

    }
    catch(ParserConfigurationException ex2)
    {

    }
    catch(SAXException ex2)
    {

    }
    
    return doc;
}

Document GetNames(String xmlUri, Map<String,String> names) 
{

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setCoalescing(true);
    dbf.setExpandEntityReferences(true);
    

    String xmlFullUri = xmlUriBase + xmlUri;

    Document doc = GetDocument(dbf, xmlFullUri);

    Node rootNode = doc.getFirstChild();

    NodeList children = rootNode.getChildNodes();

    for(int i = 0; i < children.getLength(); i++)
    {
      Node aNode = children.item(i);
      NamedNodeMap attrs = aNode.getAttributes();
      String id = attrs.getNamedItem("id").getNodeValue();
      String name = attrs.getNamedItem("title").getNodeValue();
      names.put(id,name);
    }
    return doc;
}
}
